/*
*  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
*  reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*  notice, this list of conditions and the following disclaimer.
*
*  2. Redistributions in binary form must reproduce the above copyright
*  notice, this list of conditions and the following discalimer in
*  the documentation and/or other materials provided with the
*  distribution.
*
*  3. The end-user documentation included with the redistribution,
*  if any, must include the following acknowledgment:
*  "This product includes software developed by the
*  Sun Microsystems, Inc. for Project JXTA."
*  Alternately, this acknowledgment may appear in the software itself,
*  if and wherever such third-party acknowledgments normally appear.
*
*  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
*  must not be used to endorse or promote products derived from this
*  software without prior written permission. For written
*  permission, please contact Project JXTA at http://www.jxta.org.
*
*  5. Products derived from this software may not be called "JXTA",
*  nor may "JXTA" appear in their name, without prior written
*  permission of Sun.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*  ====================================================================
*
*  This software consists of voluntary contributions made by many
*  individuals on behalf of Project JXTA.  For more
*  information on Project JXTA, please see
*  <http://www.jxta.org/>.
*
*  This license is based on the BSD license adopted by the Apache Foundation.
*
*  $Id: Dialog.java,v 1.25 2007/06/10 21:15:10 nano Exp $
*/

package net.jxta.myjxta.dialog;

import net.jxta.document.Advertisement;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredTextDocument;
import net.jxta.endpoint.Message;
import net.jxta.logging.Logging;
import net.jxta.myjxta.MyJXTA;
import net.jxta.myjxta.View;
import net.jxta.myjxta.dialog.util.Text;
import net.jxta.myjxta.misc.beam.BeamDialog;
import net.jxta.myjxta.plugin.PluginView;
import net.jxta.myjxta.util.Group;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;

import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author james todd [gonzo at jxta dot org]
 * @author mike mcangus [mcangus at jxta dot org]
 * @version $Id: Dialog.java,v 1.25 2007/06/10 21:15:10 nano Exp $
 * @modified 2005-03-27 jamoore add vojxtaDialog to dialog types
 * @modified 2005-04-24 jamoore add vijxtaDialog to dialog types
 */

public abstract class Dialog {
    /**
     * describes the number of retries a dispatch call is given after failure
     */
    private static final int DEFAULT_DISPATCH_RETRY_METRIC = 5;
    private static final int MIN_DISPATCH_RETRY_METRIC = 1;
    private static final int MAX_DISPATCH_RETRY_METRIC = 100;
    private int dispatchRetryMetric = DEFAULT_DISPATCH_RETRY_METRIC;
    protected FilterChain inbound = null;
    protected FilterChain outbound = null;
    /**
     * The list of registered DialogListener objects
     */
    private List<DialogListener> listeners = null;
    /**
     * The DialogMesage used as the template for all DialogMessage object
     * originated by this Dialog object
     */
    protected DialogMessage dialogMessage = null;
    /**
     * Are we currently connected to a pipe and can send messages
     */
    private boolean isConnected = false;//true if the GUI-Panel was already closed
    protected boolean m_closed = false;
    /**
     * The tag prefix for all myxta2 Message tags
     */
    public static final String IMFREE = "Jxta:IMFree";
    /**
     * The Message tag that denotes the PeerGroup ID of the originator
     * of a DialogMessage object
     */
    public static final String IMFREE_GROUP_ID = Dialog.IMFREE + "GroupId";
    /**
     * The Message tag that denotes the PeerGroup name of the originator
     * of a DialogMessage object
     */
    public static final String IMFREE_GROUP_NAME = Dialog.IMFREE + "GrpName";
    /**
     * The Message tag that denotes the name of the originator
     * of a DialogMessage object
     */
    public static final String IMFREE_ORIGINATOR_NAME = Dialog.IMFREE + "SenderName";
    /**
     * The Message tag that denotes the actual message part
     * of a DialogMessage object
     */
    public static final String IMFREE_MESSAGE = Dialog.IMFREE + "Message";
    /**
     * The Message tag that denotes the legacy message part
     * of a DialogMessage object
     */
    public static final String IMFREE_LEGACY_MESSAGE = Dialog.IMFREE + "SenderMessage";
    /**
     * The  String that is prefixed to the peer name to indicate a
     * PipeAdvertisment specific to myjxta
     */
    public static final String IMFREE_USER_NAME = Dialog.IMFREE + "UserName";
    /**
     * The delimiter that indictates the end of a prefix specific to
     * myjxta
     */
    public static final String IMFREE_DELIMITER = ".";
    /**
     * The Message tag that denotes the command part
     * of a DialogMessage object
     */

    public static final String IMFREE_COMMAND = Dialog.IMFREE + "Command";
    public static String DIALOG_NAME = Dialog.IMFREE_USER_NAME;
    protected Group group = null;
    protected PipeAdvertisement pipeAdvertisement = null;
    protected MyJXTA myjxta = null;
    protected static final Logger LOG = Logger.getLogger(Dialog.class.getName());
    public static final String GOODBYE_CMD = "GOODBYE";
    private static final HashMap<String, DialogNamer> dialogNamers = new HashMap<String, DialogNamer>();

    public Dialog(MyJXTA myjxta, PipeAdvertisement pa, Group group) {
        this.myjxta = myjxta;
        pipeAdvertisement = pa;
        this.group = group;
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin Dialog(Group, PipeAdvertisement, MyJXTA) Constructor");
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   Dialog(Group, PipeAdvertisement, MyJXTA) Constructor");
        }


        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin AbstractDialog(Group, MyJXTA) Constructor");
        }

        PeerGroup pg = getGroup().getPeerGroup();

        // initialize the template DialogMessage object
        this.dialogMessage = new DialogMessage(pg.getPeerName(), null,
                pg.getPeerGroupID().toString(), pg.getPeerGroupName());
        this.inbound = new FilterChain();
        this.outbound = new FilterChain();

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   AbstractDialog(Group, MyJXTA) Constructor");
        }

    }


    /**
     * Returns the current peer group name.
     *
     * @return The current peer group name.
     */
    public String getName() {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In getName()");
        }

        return getGroup().getPeerGroup().getPeerGroupName();
    }

    /**
     * Returns the current <code>FilterChain</code> that process all inbound <code>DialogMessage</code>s.
     *
     * @return The current <code>FilterChain</code> that process all inbound <code>DialogMessage</code>s.
     */
    public FilterChain getInboundFilters() {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In getInboundFilters()");
        }

        return this.inbound;
    }

    /**
     * Adds a <code>DialogFilter</code> to the current <code>FilterChain</code> that process all inbound
     * <code>DialogMessage</code>s.
     *
     * @param filter The filter to be added to the current chain.
     */
    public void addInboundFilter(DialogFilter filter) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In addInboundFilter(DialogFilter)");
        }

        this.inbound.addFilter(filter);
    }

    /**
     * Removes a <code>DialogFilter</code> from the current <code>FilterChain</code> that process all inbound
     * <code>DialogMessage</code>s.
     *
     * @param filterClass The <code>Class</code> of the filter to remove from the current chain.
     */
    public void removeInboundFilter(Class filterClass) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In removeInboundFilter(Class)");
        }

        this.inbound.removeFilter(filterClass);
    }

    /**
     * Returns the current <code>FilterChain</code> that process all outbound <code>DialogMessage</code>s.
     *
     * @return The current <code>FilterChain</code> that process all outbound <code>DialogMessage</code>s.
     */
    public FilterChain getOutboundFilters() {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In getOutboundFilters()");
        }

        return this.outbound;
    }

    /**
     * Adds a <code>DialogFilter</code> to the current <code>FilterChain</code> that process all outbound
     * <code>DialogMessage</code>s.
     *
     * @param filter The filter to be added to the current chain.
     */
    public void addOutboundFilter(DialogFilter filter) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In addOutboundFilter(DialogFilter)");
        }

        this.outbound.addFilter(filter);
    }

    /**
     * Removes a <code>DialogFilter</code> from the current <code>FilterChain</code> that process all outbound
     * <code>DialogMessage</code>s.
     *
     * @param filterClass The <code>Class</code> of the filter to remove from the current chain.
     */
    public void removeOutboundFilter(Class filterClass) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In removeOutboundFilter(Class)");
        }

        this.outbound.removeFilter(filterClass);
    }

    /**
     * Returns the current <code>List</code> of {@link net.jxta.myjxta.dialog.DialogListener}s that process all inbound and outbound
     * <code>DialogMessage</code>s.
     *
     * @return The current list of listeners.
     */
    public List getListeners() {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In getListeners()");
        }

        return this.listeners != null ?
                this.listeners : Collections.EMPTY_LIST;
    }

    /**
     * Add a new <code>DialogListener</code> object to the {@link java.util.List} of listeners being maintained for this session.
     *
     * @param listener the new DialogListener object to add
     */
    public void addListener(DialogListener listener) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin addListener(DialogListener)");
            LOG.fine("listener.getClass() = " + listener.getClass());
        }

        if (listener != null) {
            // create the listeners array if necessary
            if (this.listeners == null) {
                this.listeners = new ArrayList<DialogListener>();
            }

            // add the listener
            synchronized (listeners) {
                this.listeners.add(listener);
            }
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   addListener(DialogListener)");
        }
    }

    /**
     * Remove a previously registered {@link net.jxta.myjxta.dialog.DialogListener} object from the current <code>List</code>
     * being maintained for this session.
     *
     * @param l the DialogListener object to remove
     */
    public void removeListener(DialogListener l) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin removeListener(DialogListener)");
            LOG.fine("listener.getClass() = " + l.getClass());
        }

        if (this.listeners != null) {
            synchronized (listeners) {
                this.listeners.remove(l);

                // if there are no more registered listeners
                // allow the listeners array to be garbage collected
                if (this.listeners.size() == 0) {
                    this.listeners = null;
                }
            }
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   removeListener(DialogListener)");
        }
    }

    /**
     * Remove a <code>List</code> of {@link net.jxta.myjxta.dialog.DialogListener} objects to the current <code>List</code>
     * being maintained for this session.
     *
     * @param listeners the List of DialogListener objects to remove
     */
    public void removeListeners(List listeners) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin removeListeners(List)");
        }

        this.listeners.removeAll(listeners);

        // if there are no more registered listeners
        // allow the listeners array to be garbage collected
        if (this.listeners.size() == 0) {
            this.listeners = null;
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   removeListeners(List)");
        }
    }

    public void clear() {
        clearListeners();
        clearCache();
    }

    /**
     * Remove all {@link net.jxta.myjxta.dialog.DialogListener} objects from the {@link java.util.List} being maintained for this session.
     */
    public void clearListeners() {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin clearListeners()");
        }

        if (this.listeners != null) {
            this.listeners.clear();
            this.listeners = null;
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   clearListeners()");
        }
    }

    public void clearCache() {
        Cache.getCache(getGroup()).clear();
    }

    /**
     * Dispatch a Message object to the intended receivers
     *
     * @param msg the Message object to send across an open pipe
     * @return true if the message was successfully dispatched, and false otherwise.
     */
    public abstract boolean dispatch(Message msg);

    /**
     * { @inheritDoc }
     */
    public void close() {
        m_closed = true;
    }

    public abstract PluginView getDialogPanel(View p_myJXTAView);//quick hack --> this DialogClass is "hot" (not a template)

    public void activate() {

    }

    /**
     * @return The number of retrys the dialog will attempt when dispatching on
     *         it's pipe.
     */
    public int getDispatchRetryMetric() {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In getDispatchRetryMetric()");
        }

        return dispatchRetryMetric;
    }

    /**
     * Sets the number of retrys attempted to dispatch on the current pipe.
     * Normally the only reason a dispatch would return false is due to a
     * null time. Usually after a timeout on connection or remote peer has
     * gone down. We need a way of notifying the local peer of such a case.
     * As is, dispatch simply will not send a message after a number of retrys.
     * This megates the purpose of reliable pipes. Notification of servere errors
     * to child classes would be enough...
     *
     * @param dispatchRetryMetric TODO describe this param
     */
    public void setDispatchRetryMetric(int dispatchRetryMetric) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin setDispatchRetryMetric(int)");
        }

        if (dispatchRetryMetric < MIN_DISPATCH_RETRY_METRIC) {
            this.dispatchRetryMetric = MIN_DISPATCH_RETRY_METRIC;
        } else if (dispatchRetryMetric > MAX_DISPATCH_RETRY_METRIC) {
            this.dispatchRetryMetric = MAX_DISPATCH_RETRY_METRIC;
        } else {
            this.dispatchRetryMetric = dispatchRetryMetric;
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   setDispatchRetryMetric(int)");
        }
    }

    /**
     * Send the indicated message to the intended receivers
     *
     * @param htmlMessage the message to send
     */
    public boolean dispatch(String htmlMessage) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin dispatch(String)");
        }

        DialogMessage dm = (DialogMessage) this.dialogMessage.clone();

        dm.setHtmlMessage(htmlMessage);

        boolean result=dispatch(dm);

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   dispatch(String)");
        }
        return result;
    }

    /**
     * Send a DialogMessage object to its intended receivers.
     * DialogMessage object with an empty message will not be send
     *
     * @param msg the DialogMessage object to send
     */
    public boolean dispatch(DialogMessage msg) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin dispatch(DialogMessage)");
            LOG.fine("msg = " + msg);
        }

        if (msg != null) {
            DialogMessageWrapper w = new DialogMessageWrapper(this, msg);
            DialogFilter df;

            for (Iterator f = this.outbound != null ?
                    this.outbound.getFilters().iterator() :
                    Collections.EMPTY_LIST.iterator();
                 f.hasNext();) {
                df = (DialogFilter) f.next();
                try {
                    w = (DialogMessageWrapper) df.filter(w);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (w != null &&
                    w.isValid()) {
                Message m = w.toMessage(this.dialogMessage);

                /** this avoids an infinite loop on dispatch if the pipe is null */
            boolean isDispatched=false;
                int i=0;
                int maxRetry=getDispatchRetryMetric();
                do {
                    i++;
                    if (dispatch(m)) {
                        isDispatched=true;
                        break;

                    } else {

                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                    }
                } while (i<maxRetry);

                // do any task necessary to do after the message was send

                postDispatch(m);
                if (!isDispatched){
                    LOG.severe("unable to send message: "+this.getName());
                }
                return isDispatched;
            }
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   dispatch(DialogMessage)");
        }
        return false;
    }

    /**
     * Called if a new DialogMessage object was received.
     * This method informs all registered DialogListener objects
     * that a new DialogMessage object was received.
     *
     * @param msg the DialogMessage object that was received
     */
    public void receive(DialogMessage msg) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin receive(DialogMessage)");
            LOG.fine("msg = " + msg);
        }
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            String msgText = Text.deleteBlankLines(Text.replace(msg.getHtmlMessage(), Text.MARKUP, Text.EMPTY));
            if (msgText == null || msgText.trim().length() > 0) {
                LOG.info(msg.getOriginator() + ": " + msgText);
            }
        }

        if (msg != null) {
            DialogMessageWrapper w = new DialogMessageWrapper(this, msg);
            DialogFilter df;

            // Apply each inbound filter to the message.
            for (Iterator f = this.inbound != null ?
                    this.inbound.getFilters().iterator() :
                    Collections.EMPTY_LIST.iterator();
                 f.hasNext();) {
                df = (DialogFilter) f.next();
                w = (DialogMessageWrapper) df.filter(w);
            }

            // Apply each Listener to the message.
            // xxx: consider threading each invocation
            if (w != null && w.isValid() && this.listeners != null) {
                synchronized (listeners) {

                    for (DialogListener listener : this.listeners) {
                        try {
                            listener.receive(w);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   receive(DialogMessage)");
        }
    }

    /**
     * Return whether this  Dialog object is connected to a pipe
     *
     * @return true if we are connected to a pipe, false otherwise
     */
    public boolean isConnected() {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In isConnected()");
        }
        return this.isConnected;
    }

    /**
     * Get the DialogMessage object that is used as a template
     * for all DialogMessage objects send via this Dialog object
     *
     * @return the DialogMessage object that is used as a template
     */
    public DialogMessage getDialogMessage() {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In getDialogMessage()");
        }
        return this.dialogMessage;
    }

    /**
     * Perform any task  that need to be performed after the
     * Message object was send
     *
     * @param msg the Message object that was send
     */
    protected abstract void postDispatch(Message msg);

    /**
     * Perform any tasks that need to be performed before all
     * registered DialogListeners are informed that a new DialogMessage
     * object was received
     *
     * @param dm the DialogMessage object that was received
     * @return the changed DialogMessage image is any
     */
    protected abstract DialogMessage preReceive(DialogMessage dm);

    /**
     * Change the connection status of this Dialog object
     *
     * @param isConnected to new connection status
     */
    protected void setIsConnected(boolean isConnected) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In isConnected(boolean)");
        }
        this.isConnected = isConnected;
    }

    /**
     * Called if a new Message object was received.
     * The Message object is first converted to a DialogMessage
     * object. Then we call {@link #preReceive(net.jxta.myjxta.dialog.DialogMessage) preReceive}
     * to perform any necessary pre-processing. After that all registered
     * DialogListener objects are informed that a new message was received
     *
     * @param msg the newly received Message object
     */
    protected void receive(Message msg) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin receive(Message)");
            LOG.fine("msg = " + msg);
        }

        DialogMessage dm = new DialogMessage(msg, this.dialogMessage.getKeys());

        receive(preReceive(dm));

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   receive(Message)");
        }
    }

    /**
     * Get a string representation of the indicated Advertisment object
     *
     * @param adv the Advertisment object for which to build the
     *            string representation
     * @return a string representation of the indicated Advertisment object
     */
    protected String advertisementToString(Advertisement adv) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin advertisementToString(Advertisement)");
        }

        StringWriter sw = new StringWriter();

        try {
            StructuredTextDocument d =
                    (StructuredTextDocument) adv.getDocument(MimeMediaType.XMLUTF8);

            d.sendToWriter(sw);
        } catch (IOException ioe) {
            if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                LOG.log(Level.SEVERE, "Caught unexpected Exception", ioe);
            }
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   advertisementToString(Advertisement)");
        }
        return (sw.toString());
    }

    /**
     * Set the template from which all DialogMessage objects
     * created by this Dialog object are build
     *
     * @param dm the new template
     */
    protected void setDialogMessage(DialogMessage dm) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In setDialogMessage(DialogMessage)");
        }

        this.dialogMessage = dm;
    }

    public static String getDialogName() {
        return null;
    }

    /**
     * Normalize the name under which to search for a PipeAdvertisement
     * for which to search for  if establishing a group chat
     *
     * @param c the Class to which to add the chat specific parameters to mark the
     *          PipeAdvertisment we are looking for as belonging to myjxta
     * @return the full name of the PipeAdvertisment label
     * @modified 2005-04-24 jamoore added vijxta dialog support
     */
    // xxx: hackary
    public static DialogNamer getDialogNamer(Class c) {
        if (Logging.SHOW_FINE && Dialog.LOG.isLoggable(Level.FINE)) {
            Dialog.LOG.fine("Begin getDialogNamer(Class)");
        }

        DialogNamer dn = null;


        if (c == OneToOneCommandDialog.class) {
            dn = new DialogNamer() {
                public String getDialogName(String s) {
                    return OneToOneCommandDialog.DIALOG_NAME +
                            IMFREE_DELIMITER + s;
                }
            };
        } else if (c == BeamDialog.class) {
            dn = new DialogNamer() {
                public String getDialogName(String s) {
                    return BeamDialog.DIALOG_NAME +
                            IMFREE_DELIMITER + s;
                }
            };
        } else {
            DialogNamer dialogNamer = dialogNamers.get(c.getName());
            if (dialogNamer != null) {
                return dialogNamer;
            }

        }


        if (Logging.SHOW_FINE && Dialog.LOG.isLoggable(Level.FINE)) {
            Dialog.LOG.fine("End   getDialogNamer(Class)");
        }
        return dn;
    }

    public Group getGroup() {
        if (Logging.SHOW_FINE && Dialog.LOG.isLoggable(Level.FINE)) {
            Dialog.LOG.fine("In getGroup()");
        }
        return this.group;
    }

    public PipeAdvertisement getPipeAdvertisement() {
        if (Logging.SHOW_FINE && Dialog.LOG.isLoggable(Level.FINE)) {
            Dialog.LOG.fine("In getPipeAdvertisement()");
        }
        return this.pipeAdvertisement;
    }

    public MyJXTA getMyJXTA() {
        if (Logging.SHOW_FINE && Dialog.LOG.isLoggable(Level.FINE)) {
            Dialog.LOG.fine("In getMyJXTA()");
        }
        return this.myjxta;
    }

    public boolean isSecure() {
        if (Logging.SHOW_FINE && Dialog.LOG.isLoggable(Level.FINE)) {
            Dialog.LOG.fine("In isSecure()");
        }
        return getPipeAdvertisement().getType().equals(PipeService.UnicastSecureType);
    }

    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }

        if (Logging.SHOW_FINE && Dialog.LOG.isLoggable(Level.FINE)) {
            Dialog.LOG.fine("Begin equals(Object)");
        }
        PipeAdvertisement pa = Dialog.class.isAssignableFrom(o.getClass()) ?
                ((Dialog) o).getPipeAdvertisement() : null;

        if (Logging.SHOW_FINE && Dialog.LOG.isLoggable(Level.FINE)) {
            Dialog.LOG.fine("End   equals(Object)");
            Dialog.LOG.fine("returning " +
                    (pa != null &&
                            pa.getID().equals(getPipeAdvertisement().getID()) &&
                            pa.getType().equals(getPipeAdvertisement().getType())));
        }
        return pa != null &&
                pa.getID().equals(getPipeAdvertisement().getID()) &&
                pa.getType().equals(getPipeAdvertisement().getType());
    }

    public int hashCode() {
        return (pipeAdvertisement != null ? pipeAdvertisement.hashCode() : 0);
    }

    public static void registerDialogNamer(Class dialogClass, DialogNamer namer) {
        dialogNamers.put(dialogClass.getName(), namer);
    }

    public static void removeDialogNamer(Class dialogClass) {
        dialogNamers.remove(dialogClass.getName());
    }
}
